/**
 * 
 */
package com.shine.icis.dto;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zsy
 *
 */
public class EFDeptTree {
	private static final long serialVersionUID = 5731513126107987489L;
	private List<EFDept> depts;
	private String rootDeptId;

	public EFDeptTree(List<EFDept> depts, String rootDeptId) {
		this.depts = depts;
		this.rootDeptId = rootDeptId;
	}
	
	public String toString() {
		if (depts == null || depts.isEmpty()) {
			return "";
		}
		Map<String, EFDept> deptMap = new HashMap<String, EFDept>();
		for (EFDept ud : depts) {
			deptMap.put(ud.getDepId(), ud);
		}
		for (EFDept ud : depts) {
			EFDept pud = deptMap.get(ud.getParentDepId());
			if (pud != null) {
				pud.addChildDept(ud);
			}
		}
		EFDept rDept = deptMap.get(rootDeptId);
		StringBuffer out = new StringBuffer();
		if (rDept == null) {
			return out.toString();
		}
		out.append("<li id='").append(rDept.getDepId())
			.append("'>").append("<span class=\"text\" >").append(rDept.getDepName()).append("</span>");
		if (rDept.getChildDepts() != null && !rDept.getChildDepts().isEmpty()) {
			out.append("<ul>");
			for (EFDept d : rDept.getChildDepts()) {
				out.append("<li id='").append(d.getDepId())
					.append("'>").append("<span class=\"text\" >").append(d.getDepName()).append("</span>");
				out.append("</li>");
			}
			out.append("</ul>");
		}
		out.append("</li>");
		
		return out.toString();
	}
}
